package com.cooba.adapter;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.cooba.R;
import com.cooba.activitys.login.LoginActivity;
import com.cooba.bean.GameBean;
import com.cooba.bean.NewsLoad;
import com.cooba.utils.ACache;
import com.cooba.utils.CommonUtil;
import com.cooba.utils.Constants;
import com.cooba.utils.SharedPreferencesUtil;
import com.cooba.utils.Utils;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.qq.e.ads.banner.ADSize;
import com.qq.e.ads.banner.AbstractBannerADListener;
import com.qq.e.ads.banner.BannerView;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import eu.janmuller.android.simplecropimage.Util;

public class GameAdapter extends BaseAdapter {
    private Activity context;
    private List<GameBean.GameInfo> list;
    private OnItemDownLoadClickListener onItemDownLoadClickListener;

    public List<GameBean.GameInfo> getList() {
        return list;
    }

    public void setList(List<GameBean.GameInfo> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public void addNewData(List<GameBean.GameInfo> datas) {
        if (datas != null && list != null) {
            this.list.addAll(0, datas);
            this.notifyDataSetChanged();
        }
    }

    public void addMoreData(List<GameBean.GameInfo> datas) {
        Log.d("wei", "game:" + datas.size() + "," + list.size());
        if (datas != null && list != null) {
            list.addAll(list.size(), datas);
            this.notifyDataSetChanged();
        }
    }

    public GameAdapter(Activity context, List<GameBean.GameInfo> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        if (list.size() == 0) {
            return 0;
        }
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolderGame holder_game = null;
        if (convertView == null) {
            holder_game = new ViewHolderGame();
            convertView = LayoutInflater.from(context).inflate(R.layout.game_list_item, parent, false);
            holder_game.game_ads_ll = (LinearLayout) convertView.findViewById(R.id.game_item_ads_ll);
            //holder_game.game_item_ads_iv = (ImageView) convertView.findViewById(R.id.game_item_ads_iv);
            holder_game.game_icon = (ImageView) convertView.findViewById(R.id.game_item_icon);
            holder_game.game_name = (TextView) convertView.findViewById(R.id.game_item_name);
            holder_game.game_integral = (TextView) convertView.findViewById(R.id.game_item_integral);
            holder_game.game_size = (TextView) convertView.findViewById(R.id.game_item_size);
            holder_game.game_star = (RatingBar) convertView.findViewById(R.id.game_item_star);
            holder_game.game_download = (TextView) convertView.findViewById(R.id.game_item_download);
            convertView.setTag(holder_game);
        } else {
            holder_game = (ViewHolderGame) convertView.getTag();
        }

        if (position % 5 == 0) {

            BannerView bv = new BannerView(context, ADSize.BANNER, Constants.APPID, Constants.GAME_Banner);
            bv.setRefresh(30);
            bv.setADListener(new AbstractBannerADListener() {
                @Override
                public void onNoAD(int i) {
                    Log.i("AD_DEMO", "BannerNoAD，eCode失败" + i);
                }

                @Override
                public void onADReceiv() {
                    Log.i("AD_DEMO", "ONBannerReceive成功");
                }
            });
            holder_game.game_ads_ll.removeAllViews();
            holder_game.game_ads_ll.addView(bv);
            bv.loadAD();
            holder_game.game_ads_ll.setVisibility(View.VISIBLE);
        } else {
            holder_game.game_ads_ll.removeAllViews();
            holder_game.game_ads_ll.setVisibility(View.GONE);
        }
        final GameBean.GameInfo appInfo = list.get(position);
        float star;
        holder_game.game_name.setText(appInfo.getGame_name());
        holder_game.game_integral.setText(appInfo.getGame_vantager());
        holder_game.game_size.setText(appInfo.getGame_size() + "M");
        if (TextUtils.isEmpty(appInfo.getGame_star())) {
            star = 5;
        } else {
            star = Float.valueOf(appInfo.getGame_star());
        }
        if (appInfo.getDownload_status() == 0) {
            holder_game.game_download.setText("下载");
        } else {
            holder_game.game_download.setText(appInfo.getProgress() + "%");
        }
        holder_game.game_star.setRating(star);
        BitmapUtils bitmapUtils = new BitmapUtils(context);
        bitmapUtils.display(holder_game.game_icon, appInfo.getGame_icon());
        CommonUtil.putTextViewMap(list.get(position).getId(), holder_game.game_download);
        final TextView textView = holder_game.game_download;
        holder_game.game_download.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (textView.getText().equals("下载")) {
                    String login_id = SharedPreferencesUtil.getData(context, "login_id", "");
                    if (!TextUtils.isEmpty(login_id)) {
                        Toast.makeText(context, "下载安装后，系统会自动增加积分，请保证本应用处于开启状态!", Toast.LENGTH_LONG).show();
                        CommonUtil.downloadAPK(context, list.get(position));
                    } else {
                        Toast.makeText(context, "登录后下载即可获取积分兑换海量商品!", Toast.LENGTH_LONG).show();
                        showWarnDialog(position);
                    }
                } else {
                    Toast.makeText(context, "已经下载", Toast.LENGTH_SHORT).show();
                }
            }
        });


        return convertView;
    }


    static class ViewHolderGame {
        public LinearLayout game_ads_ll;
        //public ImageView game_item_ads_iv;
        public ImageView game_icon;
        public TextView game_name;
        public TextView game_integral;
        public TextView game_size;
        public RatingBar game_star;
        public TextView game_download;
    }


    public void setOnItemDownLoadClickListener(OnItemDownLoadClickListener onItemDownLoadClickListener) {
        this.onItemDownLoadClickListener = onItemDownLoadClickListener;
    }

    public interface OnItemDownLoadClickListener {
        void download(View v, int position, GameBean.GameInfo appinfo);
    }

    public void showWarnDialog(final int pos) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("尚未登录,是否登录?");
        builder.setNegativeButton("否", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                CommonUtil.downloadAPK(context, list.get(pos));
            }
        });
        builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(context, LoginActivity.class);
                context.startActivity(intent);
            }
        });
        AlertDialog dialog = builder.create();
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();

    }

    /**
     * 局部刷新
     *
     * @param view
     * @param itemIndex
     */
    public void updateView(View view, int itemIndex,boolean flag) {
        if (view == null) {
            return;
        }
        //从view中取得holder
        ViewHolderGame holder = (ViewHolderGame) view.getTag();
        holder.game_name= (TextView) view.findViewById(R.id.game_item_name);
        Log.d("wei",""+holder.game_name.getText());
        holder.game_download = (TextView) view.findViewById(R.id.game_item_download);

        setData(holder, itemIndex,flag);
    }

    /**
     * 设置viewHolder的数据
     *
     * @param holder
     * @param itemIndex
     */
    private void setData(ViewHolderGame holder, int itemIndex,boolean flag) {
        int progress;
        if (flag){
            progress=list.get(itemIndex-1).getProgress();
        }else {
            progress = list.get(itemIndex).getProgress();
        }
        Log.d("wei",""+progress);
        if (progress<100) {
            holder.game_download.setText(progress + "%");
        } else {
            holder.game_download.setText("完成");
        }
    }
}
